package com.karry.mapper;

import com.karry.pojo.Comment;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;

public interface CommentMapper {

    //获取某个用户全部未查看通知（暂时只有被回复或者评论通知）
    @Select("select * from tb_comment where c_back_user = #{bUID} and c_status = 1 and c_user_id != 0 and c_notify = 1;")
    @ResultMap("commentResultMap")
    ArrayList<Comment> searchNotify(int bUID);

    //清除未读标记
    @Update("update tb_comment set c_notify = 0 where c_blog_id = #{bId} and c_back_user = #{bUId};")
    int updateNotify(@Param("bId") int bId, @Param("bUId") int bUId);

    //清除未读标记 by id
    @Update("update tb_comment set c_notify = 0 where c_id = #{id};")
    int updateNotifyByID(int id);

    //查询某篇全部一级评论
    @Select("select * from tb_comment where c_blog_id = #{id} and c_father_id = 0 and c_user_id != 0 and c_status = 1;")
    @ResultMap("commentResultMap")
    ArrayList<Comment> searchBlogFatherComment(int id);

    //查询某个一级评论全部回复
    @Select("select * from tb_comment where c_father_id = #{id} and c_status = 1;")
    @ResultMap("commentResultMap")
    ArrayList<Comment> searchBackComment(int id);

    //插入评论，返回主键
    int insertComment(@Param("comment") Comment comment);

    //根据id获取comment
    @Select("select * from tb_comment where c_id = #{id};")
    @ResultMap("commentResultMap")
    Comment searchCommentByID(int id);

    //管理页面查询总数
    @Select("select count(*) from tb_comment;")
    int searchCommentSize();

    //分页查询
    @Select("select * from tb_comment limit #{index}, #{pageSize};")
    @ResultMap("commentResultMap")
    ArrayList<Comment> searchCommentByPage(@Param("index") int index, @Param("pageSize") int size);

    //删除评论
    @Update("update tb_comment set c_status = 0 where c_id = #{id};")
    int deleteComment(int id);

    //恢复评论
    @Update("update tb_comment set c_status = 1 where c_id = #{id};")
    int liveComment(int id);

    //查询全部
    @Select("select * from tb_comment;")
    @ResultMap("commentResultMap")
    ArrayList<Comment> searchAllComment();

    //获取用户通知（第一条）
    @Select("select * from tb_comment where c_back_user = #{id} and c_blog_id = 0 and c_user_id = 0 and c_notify = 1 and c_status = 1 limit 0, 1;")
    @ResultMap("commentResultMap")
    Comment searchFirstNotify(int id);

}
